package ds.list;

import ds.exception.EmptyException;
import ds.exception.PosIllegal;

import java.util.Arrays;
import java.util.Comparator;

public class MySingleList<T> implements IList<T> {

    private static class ListNode<T> {
        public T val;
        public ListNode<T> next;
        public ListNode(T val) {
            this.val = val;
        }
    }

    private ListNode<T> head;
    private int valSize;

    public MySingleList() {}

    //头插法
    public void addFirst(T data) {
        if (isEmpty()) {
            addOneNode(data);
        } else {
            ListNode<T> newHead = new ListNode<>(data);
            newHead.next = head;
            head = newHead;
        }
        valSize++;
    }

    // 添加第一个节点
    public void addOneNode(T data) {
        head = new ListNode<>(data);
    }

    //尾插法
    public void addLast(T data) {
        if (isEmpty()) {
            addOneNode(data);
        } else {
            ListNode<T> cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = new ListNode<>(data);
        }
        valSize++;
    }

    private void checkIndex(int index) {
        if (index < 0 || index > valSize) {
            throw new PosIllegal("位置不合法！");
        }
    }
    private void checkIndexDel(int index) {
        if (index < 0 || index > valSize - 1) {
            throw new PosIllegal("位置不合法！");
        }
    }

    ///////////////////////////////////////////////////
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,T data) {
        try {
            checkIndex(index);

            if (index == 0) {
                addFirst(data);
            } else if (index == valSize) {
                addLast(data);
            } else {
                ListNode<T> cur = head;
                int tmp = index;
                while (tmp > 0) {
                    cur = cur.next;
                    tmp -- ;
                }

                ListNode<T> newNode = new ListNode<>(data);
                ListNode<T> curNext = cur.next;
                cur.next = newNode;
                newNode.next = curNext;
                valSize++;
            }
        } catch (PosIllegal e) {
            e.printStackTrace();
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(Object key) {
        try {
            checkEmpty();

            ListNode<T> cur = head;
            while (cur != null) {
                if (cur.val.equals(key)) {
                    return true;
                }
                cur = cur.next;
            }
        } catch (EmptyException e) {
            e.printStackTrace();
        }
        return false;
    }

    //头删
    public boolean removeFirst() {
        return remove(0);
    }
    //尾删
    public boolean removeLast() {
        return remove(valSize - 1);
    }
    //删除第一次出现关键字为key的节点
    public boolean remove(Object key) {
        try {
            checkEmpty();

            ListNode<T> cur = head;
            if (head.val.equals(key)) {
                head = head.next;
                cur.next = null;
                valSize -- ;
                return true;
            }

            while (cur.next != null) {
                if (cur.next.val.equals(key)) {
                    cur.next = cur.next.next;
                    valSize --;
                    return true;
                }
                cur = cur.next;
            }
        } catch (EmptyException e) {
            e.printStackTrace();
        }
        return false;
    }
    //删除所有值为key的节点
    public boolean removeAllKey(Object key) {
        int flag = 0;
        try {
            checkEmpty();

            ListNode<T> prev = head;
            ListNode<T> cur = head.next;

            while (cur != null) {
                if (cur.val.equals(key)) {
                    prev.next = cur.next;
                    cur = cur.next;
                    flag ++;
                    valSize --;
                } else {
                    prev = cur;
                    cur = cur.next;
                }
            }
            if (head.val.equals(key)) {
                head = head.next;
                flag ++;
                valSize --;
            }
        } catch (EmptyException e) {
            e.printStackTrace();
        }

        return flag != 0;
    }
    // 删除下标为index的节点
    public boolean remove(int index) {
        try {
            checkEmpty();
            checkIndexDel(index); // ? 如果抛异常还会不会执行后面的代码

            if (index == 0) {
                head = head.next;
            } else {
                ListNode<T> cur = head;
                ListNode<T> prev = head;
                while (index > 0) {
                    prev = cur;
                    cur = cur.next;
                    index--;
                }

                prev.next = cur.next;
            }
            valSize --;
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return false;
    }
    ///////////////////////////////////////////////////

    //得到单链表的长度
    public int size() {
        return valSize;
    }
    public void clear() {
        if (isEmpty()) {
            return;
        }

        ListNode<T> cur = head;
        while (cur != null) {
            ListNode<T> next = cur.next;
            cur.next = null;
            cur = next;
        }
        valSize = 0;
        head = null;
    }
    public String toString() {
        StringBuilder sb = new StringBuilder();
        ListNode<T> cur = head;
        sb.append('[');
        while (cur != null) {
            if (cur.next != null) {
                sb.append(cur.val);
                sb.append(", ");
            } else {
                sb.append(cur.val);
            }
            cur = cur.next;
        }
        sb.append(']');
        return sb.toString();
    }
    public boolean isEmpty() {
        return head == null;
    }

    private void checkEmpty() {
        if (isEmpty()) {
            throw new EmptyException("链表为空！");
        }
    }
}